//
//  BaseViewController.m
//  iOS
//
//  Created by mokai on 14/11/19.
//  Copyright (c) 2014年 mokai. All rights reserved.
//
#import "BaseViewController.h"
#define ANIMATIONDURATION 0.30f
#define KEYBOARD_Y_OFFSET 60

@interface BaseViewController ()
@property (nonatomic,strong) UITextField *oldTextField;

@end

@implementation BaseViewController{
    BOOL _wasKeyboardManagerEnabled;
    BOOL _keyboardIsVisible;
}
@synthesize sView;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.sView = [[UIView alloc] initWithFrame:content_rect];
    self.sView.tag = 1;
    [self.view addSubview:self.sView];
}

#pragma mark 键盘相关
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    _wasKeyboardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center  addObserver:self selector:@selector(keyboardDidShow)  name:UIKeyboardDidShowNotification  object:nil];
    [center addObserver:self selector:@selector(keyboardDidHide)  name:UIKeyboardWillHideNotification object:nil];
    _keyboardIsVisible = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    //隐藏键盘
    [self.view endEditing:YES];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    _oldTextField = textField;
    CGRect frame =  [self.view convertRect:textField.frame fromView:[textField superview]];
    int offset = frame.origin.y + KEYBOARD_Y_OFFSET - (self.view.frame.size.height - 216.0);//键盘高度216
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0){
        [UIView animateWithDuration:ANIMATIONDURATION animations:^{
            self.sView.top = -offset;
        }];
    }
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.returnKeyType) {
        case UIReturnKeyNext:
            [self textFieldNext:textField];
            break;
        default:
            [self textFieldReturn:textField];
            [textField resignFirstResponder];
            break;
    }
    return  YES;
}

-(void)textFieldNext:(UITextField *)textField{
    UIView *view = [textField.superview viewWithTag:textField.tag+1];
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *next = (UITextField *)view;
        [next becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
}

-(void)textFieldReturn:(UITextField *)textField{
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return  YES;
}


//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (!_keyboardIsVisible) {
        [UIView animateWithDuration:ANIMATIONDURATION animations:^{
            self.sView.top = content_y;
        }];
    }
}

//点击空白
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)keyboardDidShow
{
    _keyboardIsVisible = YES;
}

- (void)keyboardDidHide
{
    _keyboardIsVisible = NO;
}
@end
